import math
from math import degrees, radians

import pyglet
from pyglet.gl import  *
from pyglet.window import key

import cocos
from cocos.actions import *
from cocos.layer import *
from cocos.scene import *
from cocos.director import director
from cocos.scenes.transitions import *
from cocos.sprite import Sprite
from cocos.particle import *
from cocos.particle_systems import *
from globalvars import *
import Box2D as box2d

from res import Images
import entity

class  Prop(entity.Entity):
    def __init__(self, world, image, *args, **kwargs):
        "Init"
        super(Prop,self).__init__(world,image, *args,**kwargs)
        self.stateMachine.setState(self.Idle(self))
        
    class Idle(entity.State):
        def Execute(self,dt):
            pass


class Bumper(entity.Entity):

    def __init__(self, world, image=Images.bumper,*args, **kwargs):
        "Init"
        super(Bumper,self).__init__(world, image, *args, **kwargs)
        
        "centerBody"
        self.centerBodyDef = box2d.b2BodyDef()
        
        "bumperBody"
        self.bumperBodyDef = box2d.b2BodyDef()
        self.bumperBodyDef.linearDamping = 0
        self.bumperBodyDef.angularDamping = 0
        self.bumperShapeDef = box2d.b2CircleDef()
        self.bumperShapeDef.radius = (self.width+self.height)/2/2/RATIO
        self.bumperShapeDef.friction = 0.2
        self.bumperShapeDef.restitution = 0.9
        self.bumperShapeDef.density = 0.01
        self.bumperShapeDef.SetUserData(self)
    
        "Create body and shape(s)"
        self.centerBody = self.world.CreateBody(self.centerBodyDef)
        self.bumperBody = self.world.CreateBody(self.bumperBodyDef)
        self.bumperShape = self.bumperBody.CreateShape(self.bumperShapeDef)
        self.bumperBody.SetMassFromShapes()
        
        "Create Joint"
        self.bumperJointDef=box2d.b2MouseJointDef() 
        self.bumperJointDef.body1 = self.centerBody#self.world.GetGroundBody()
        self.bumperJointDef.body2 = self.bumperBody
        self.bumperJointDef.collideConnected = False
        self.bumperJointDef.dampingRatio = 0#.9
        self.bumperJointDef.maxForce = 900* self.bumperBody.GetMass()
    
    def initJoint(self):
        self.bumperJointDef.target = self.centerBody.position
        self.bumperJoint = self.world.CreateJoint(self.bumperJointDef).getAsType()
        self.bumperBody.WakeUp()
        
    def update(self, dt):
        "Update the sprite pos and rot based on its b2Body"
        self.stateMachine.update(dt)
        self.position = (self.bumperBody.position * RATIO).tuple()
        self.rotation = -(degrees(self.bumperBody.angle))
        
            
class MoveableBar(entity.Entity):

    def __init__(self, world, image=Images.ground_box,*args, **kwargs):
        "Init"
        super(MoveableBar,self).__init__(world, image, *args, **kwargs)
        self.world = world
        
        "BodyDef"
        self.bodyDef = box2d.b2BodyDef()
        self.bodyDef.linearDamping = 0.6
        self.bodyDef.angularDamping = 0.6
        
        "ShapeDef"
        self.boxShapeDef = box2d.b2PolygonDef()
        self.boxShapeDef.SetAsBox(self.width/RATIO/2., self.height/RATIO/2., (0,0),0)
        self.boxShapeDef.friction = 0.5
        self.boxShapeDef.restitution = 0.6
        self.boxShapeDef.density = 0.3
        self.boxShapeDef.SetUserData(self)
    
        "Create body and shape(s)"
        self.body = self.world.CreateBody(self.bodyDef)
        self.boxShape = self.body.CreateShape(self.boxShapeDef)
        self.body.SetMassFromShapes()
        
    def update(self, dt):
        "Update the sprite pos and rot based on its b2Body"
        self.stateMachine.update(dt)
        self.position = (self.body.position * RATIO).tuple()
        self.rotation = -(degrees(self.body.angle))
        
